import 'package:flutter/material.dart';

import 'package:flutter_deer/res/colors.dart';

class PressBlueButton extends StatefulWidget {
  final String text;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress; // 新增：长按回调
  final bool isEnabled;

  // 颜色参数
  final Color defaultColor;
  final Color pressedColor;
  final Color hoverColor;
  final Color disabledColor;

  // 字体大小参数，默认为 14
  final double fontSize;

  // 高度参数，如果为 null 则自适应，否则使用指定高度
  final double? height;

  // 宽度参数，如果为 null 则自适应，否则使用指定宽度
  final double? width;

  PressBlueButton({
    super.key,
    required this.text,
    required this.onPressed,
    this.onLongPress, // 新增参数
    this.isEnabled = true,
    this.defaultColor = Colours.blue4B7EFE,
    this.pressedColor = const Color.fromRGBO(75, 126, 254, 0.8),
    this.hoverColor = Colours.blue6BA2FF,
    this.disabledColor = Colors.grey,
    this.fontSize = 14.0,
    this.height,
    this.width,
  });

  @override
  State<PressBlueButton> createState() => _PressBlueButtonState();
}

class _PressBlueButtonState extends State<PressBlueButton> {
  bool _isPressed = false;
  bool _isHovered = false;

  @override
  Widget build(BuildContext context) {
    Color currentColor;

    if (!widget.isEnabled) {
      currentColor = widget.disabledColor;
    } else if (_isPressed) {
      currentColor = widget.pressedColor;
    } else if (_isHovered) {
      currentColor = widget.hoverColor;
    } else {
      currentColor = widget.defaultColor;
    }

    Widget button = MouseRegion(
      onEnter: (_) {
        if (widget.isEnabled) {
          setState(() => _isHovered = true);
        }
      },
      onExit: (_) {
        setState(() => _isHovered = false);
      },
      child: GestureDetector(
        onTapDown: (_) {
          if (widget.isEnabled) {
            setState(() => _isPressed = true);
          }
        },
        onTapUp: (_) {
          if (widget.isEnabled) {
            setState(() => _isPressed = false);
          }
        },
        onTapCancel: () {
          if (widget.isEnabled) {
            setState(() => _isPressed = false);
          }
        },
        // 新增：处理长按事件
        onLongPress: widget.onLongPress != null && widget.isEnabled
            ? () {
                setState(() => _isPressed = false);
                widget.onLongPress!();
              }
            : null,
        onTap: widget.isEnabled ? widget.onPressed : null,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 150),
          width: widget.width, // 使用指定宽度参数
          height: widget.height, // 使用指定高度参数
          padding: const EdgeInsets.only(left: 8, right: 8, top: 5, bottom: 5),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: currentColor,
            borderRadius: BorderRadius.circular(3), // 稍微增加圆角
            boxShadow: widget.isEnabled
                ? [
                    BoxShadow(
                      color: _isHovered ? currentColor.withOpacity(0.3) : Colors.transparent,
                      blurRadius: 4.0,
                      offset: const Offset(0, 2),
                    )
                  ]
                : [], // 禁用时无阴影
          ),
          child: Text(widget.text, style: TextStyle(fontSize: widget.fontSize, color: Colors.white, fontWeight: FontWeight.w500)),
        ),
      ),
    );
    return button;
  }
}
